import { defineStore } from 'pinia';
import { messageFn } from '../../utils';

import {
    reducedCreateApi,//创建费用减免账单
    reducedRecordApi,//获取费用减免账单数据
    costReducedRecordApi,//获取费用减免记录
} from '/@/api/comprehensive/costReduction';

import { useLoading } from '/@/components/Loading';

const [openFullLoading, closeFullLoading] = useLoading({
    tip: '加载中...',
});


export interface costReductionTableType{
    user_id : string,
    user_code : string,
    user_name : string,
    payment_items: Array<paymentItemsType>,
    item_data_list: Array<itemDataListType>,
    total_amount : number
}

export interface paymentItemsType{
    water_item_id : number,
    amount_type : string,
    amount : number,
    typeName?:string,
    newAmount?:number
}

export interface itemDataListType{
    ladder_sn : number,
    quota_min : number,
    quota_max : number,
    ladder_water_qty : number,
    ladder_price : number,
    real_ladder_price?: any,//暂未确定类型
    should_pay_amount?: any,//暂未确定类型
    billing_record_id : string,
    detail_id : string,
    water_item_id : number,
    water_item_name : string,
    item_total_water?: any,//暂未确定类型
    water_usage_id : number,
    water_usage_name : string,
    ladder_amount : number,
    ladder_id : string
}


interface costReductionFace {
    reducedRecordList: Array<costReductionTableType>; //创建费用减免账单列表结果
    costReducedRecordList: [];//获取费用减免记录结果
    testData : [],//测试
    total: number;
    pageInfo: object;
}

export const costReductionStore = defineStore({
    id: 'costReductionConfig-state',
    state: (): costReductionFace => ({
        reducedRecordList: [], //创建费用减免账单列表结果
        costReducedRecordList: [], //获取费用减免记录结果
        testData : [], //测试
        total: 0,//总页数
        pageInfo: {
            //分页
            page_no: 1,
            page_size: 10,
        },
    }),
    getters: {

    },
    actions: {
        setTotal(total: number) {
            console.log('setTotal----', total);
            //总页数
            this.total = total;
        },
        setPageInfo(pageParams: object) {
            //设置页面数据
            this.pageInfo = pageParams;
        },
        setTestData(td){
            this.testData = td;
        },
        //设置费用减免账单列表结果
        setReducedRecordList(recordList: Array<costReductionTableType>) {
            this.reducedRecordList = recordList;
        },
        //设置费用减免记录
        setCostReducedRecordList(recordList: []) {
            console.log('costReducedRecordList--123--',recordList);
            this.costReducedRecordList = recordList;
        },

/*
* @description: costReducedRecordApi //获取费用减免记录
*/
        async costReducedRecordAction(params: object) {
            openFullLoading();
            try{
                const costReducedRecord = await costReducedRecordApi(params);
                if (costReducedRecord.code === 0) {
                    this.setCostReducedRecordList(costReducedRecord.data);
                    return true;
                }else{
                    messageFn('error', costReducedRecord.msg);
                    return false;
                } 
            }finally{
                closeFullLoading();
            }
           
        },

/* @description: reducedRecordApi //获取费用减免账单数据
*/
        async reducedRecordAction(params: object) {
            openFullLoading();
            try{
                const reducedRecord = await reducedRecordApi(params);
                if (reducedRecord.code === 0) {
                    this.setReducedRecordList(reducedRecord.data);
                    return true;
                }else{
                    messageFn('error', reducedRecord.msg);
                    return false;
                } 
            }finally{
                closeFullLoading();
            }
           
        },
        /**
* @description:  //创建费用减免账单
*/
        async reducedCreateAction(params: object) {
            openFullLoading()
            try {
                const reducedCreate = await reducedCreateApi(params);
                if (reducedCreate.code === 0) {
                    messageFn('success', reducedCreate.msg);
                    return true;
                } else {
                    messageFn('error', reducedCreate.msg);
                    return false;
                }
            } finally {
                closeFullLoading()
            }
        },
    }
});
